package com.keya.features.job;

import com.keya.config.JobConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import javax.annotation.Resource;

@Configuration
@EnableScheduling
@Slf4j
public class ClearDataJob implements SchedulingConfigurer {

    @Resource
    JobConfig jobConfig;


    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(
                () -> {
                    try {
                        job();
                    } catch (Exception e) {
                        log.error("清理定时异常", e);
                    }
                },
                triggerContext -> {
                    String cron = jobConfig.getClearData();
                    return new CronTrigger(cron).nextExecutionTime(triggerContext);
                }
        );
    }

    public void job() {
    }


}
